using Microsoft.AspNetCore.Mvc;
using mvc.Models;
using mvc.Responitory;
using System.Collections.Generic;
using System.Linq;
namespace AspNetCoreMVC.Controllers
{
    public class ProductController : Controller
    {
        Responitory<Product> CRUD = new Responitory<Product>();
        public IActionResult Index()
        {
            Responitory<Category> insert = new Responitory<Category>();
            if (insert.GetTable.FirstOrDefault() == null)
            {
                Category category = new Category
                {
                    CategoryName = "闽大"
                };
                insert.InsertData(category);
            }
            var Entitys = (from Tb in CRUD.GetTable where Tb.IsDeleted == false select Tb) as IEnumerable<Product>;
            return View(Entitys);
        }

        public IActionResult Delete(int id)
        {
            CRUD.DeleteData(id);
            return Ok();
        }
        public IActionResult Update(int id)
        {
            if (id > 0)
            {
                var Entity = CRUD.CheckData(id);
                return View(Entity);
            }
            return View();

        }
        public IActionResult Save(Parm obj)
        {
            if (obj.saveID > 0)
            {
                var Entity = (from Tb in CRUD.GetTable where Tb.IsDeleted == false select Tb).FirstOrDefault();
                Entity.Id = obj.saveID;
                Entity.Price = obj.savePrice;
                Entity.ProductName = obj.saveProductName;
                Entity.Remarks = obj.saveIsaveRemarksD;
                Entity.Stock = obj.saveStock;
                Entity.Supplier = obj.saveSupplier;
                CRUD.UpateData(Entity);
                return Ok();
            }
            else
            {
                var entity =new Product{
                    Price = obj.savePrice;
                };
            }
            return Ok();
        }

    }
}

